package com.easywan.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.vo.Admin;

@Component
public class AdminInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		boolean result = false;
		
		HttpSession session = arg0.getSession();
		String uri = arg0.getRequestURI();
		
		if(uri.indexOf("login") > 0){
			result = true;
		}else{
			//检查权限
			Admin user = (Admin) session.getAttribute("admin");
			if(null == user){
				result = false;
			}else{
				result = true;
			}
		}
		
		return result;
	}
	
}
